<?php
	/*
	 *
	 *
	 */
	
	require_once("globals.inc.php");
	
	$sqlhandle = mysqli_init();
	
	function sqlQuery($qry){
		global $sqlhandle;
		
		if ($sqlhandle->multi_query($qry)) {
			$results_array = array();
			do {
				if ($result = $sqlhandle->store_result()) {
					while ($row = $result->fetch_row()) array_push($results_array, $row);
					$result->free();
				}
			} while ($sqlhandle->next_result());
		}
		if(empty($results_array)) $results_array[0] = 1;
		return $results_array;
	}	
	
	function sqlConnect(){
		global $database_host, $database_user, $database_passwd, $sqlhandle;
	
		$sqlhandle->real_connect($database_host, $database_user, $database_passwd);
		if ($sqlhandle->connect_errno) {
			echo "Failed to connect to MySQL: (" . $sqlhandle->connect_errno . ") " . $sqlhandle->connect_error;
		}
	}

	function sqlClose(){
		global $sqlhandle;
	
		mysqli_close($sqlhandle);
	}
	
	function sqlInitAndFill(){
		global $database_db, $database_prefix, $sqlhandle;
		
		$qry = "DROP DATABASE IF EXISTS " . $database_db;
		$qry .= "; CREATE DATABASE " . $database_db;
		$qry .= "; USE " . $database_db;
		$qry .= "; CREATE TABLE " . $database_prefix . "_poi(P_Id int NOT NULL AUTO_INCREMENT, POILocationCode int NOT NULL, POIName varchar(255), POIDesc varchar(5000), POIImgLink varchar(255), POILocation varchar(255), PRIMARY KEY (P_Id))";
		$qry .= "; CREATE TABLE " . $database_prefix . "_users(P_Id int NOT NULL AUTO_INCREMENT, USRPrivileges int NOT NULL, USRName varchar(255), PRIMARY KEY (P_Id))";
		$qry .= "; CREATE TABLE " . $database_prefix . "_region(P_Id int NOT NULL AUTO_INCREMENT, REGIONCode int NOT NULL, REGIONName varchar(255), PRIMARY KEY (P_Id))";
		$res =  sqlQuery($qry);
		var_dump($res);
	}
	
	function sqlSelectDB(){
		global $database_db, $sqlhandle;
		
		$qry = "USE " . $database_db;
		sqlQuery($qry);
	}
	
	function sqlNumberOfRows($table){
		global $sqlhandle;
		
		$qry = "SELECT P_Id FROM " . $table;
		$stmt = $sqlhandle->prepare($qry);
		$stmt->execute();
		$stmt->store_result();
		$num = $stmt->num_rows;
		$stmt->close();
		
		return $num;
	}
?>